package com.nc.delivery_project.client.schedules;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;
import com.nc.delivery_project.client.add_schedule.AddSchedulePlace;
import com.nc.delivery_project.client.schedule_details.ScheduleDetailsPlace;
import com.nc.delivery_project.shared.proxy.ScheduleProxy;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * Created by DRAGON on 18.01.2015.
 */
public class SchedulesView extends Composite {

    interface ScheduleViewUiBinder extends UiBinder<VerticalPanel, SchedulesView> {
    }

    private static ScheduleViewUiBinder uiBinder = GWT.create(ScheduleViewUiBinder.class);

    private ListDataProvider<ScheduleProxy> dataProvider;

    private List<ScheduleProxy> dataList;

    ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler;

    @UiField
    VerticalPanel panel;

    @UiField
    TextBox filterTxtBox;

    @UiField
    DataGrid<ScheduleProxy> schedulesDataGrid;

    @UiField
    SimplePager pager;

    @UiField
    Button addBtn;

    private SchedulesPresenter presenter;

    public SchedulesView() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    public void setPresenter(SchedulesPresenter presenter) {
        this.presenter = presenter;
    }

    public void start() {
        panel.setCellWidth(schedulesDataGrid, "100%");
        panel.setCellWidth(pager, "100%");

        schedulesDataGrid.setWidth("100%");
        schedulesDataGrid.setHeight("400px");
        schedulesDataGrid.setEmptyTableWidget(new HTML("No Data to Display"));

        pager.setDisplay(schedulesDataGrid);

        dataProvider = new ListDataProvider<>();
        dataProvider.setList(new ArrayList<ScheduleProxy>());

        sortHandler = new ColumnSortEvent.ListHandler<>(dataProvider.getList());

        initTableColumns(schedulesDataGrid, sortHandler);

        schedulesDataGrid.addColumnSortHandler(sortHandler);

        dataProvider.addDataDisplay(schedulesDataGrid);

        filterTxtBox.addKeyUpHandler(new KeyUpHandler() {           @Override
            public void onKeyUp(KeyUpEvent event) {
       //        filter(filterTxtBox.getText());
            }
        });

        addBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                presenter.goTo(new AddSchedulePlace(""));
            }
        });
    }
//    private void filter(String filteredCurrency) {
//        List<ScheduleProxy> filteredSchedules = new ArrayList<>();
//        for (ScheduleProxy schedule : dataList) {
//          if (schedule.getCurrency().contains(filteredCurrency)) {
//                filteredSchedules.add(schedule);
//           }
//       }
//
//        bindData(filteredSchedules);
//    }

    public void initTableColumns(DataGrid<ScheduleProxy> dataGrid, ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> idCol = createIdCol(sortHandler);
        Column<ScheduleProxy, String> originCol = createOriginCol(sortHandler);
        Column<ScheduleProxy, String> destinationCol = createDestinationCol(sortHandler);
        Column<ScheduleProxy, String> primeCostCol = createPrimeCostCol(sortHandler);
        Column<ScheduleProxy, String> sellingRateCol = createSellingRateCol(sortHandler);
        Column<ScheduleProxy, String> departureDateCol = createDepartureDateCol(sortHandler);
        Column<ScheduleProxy, String> arrivalDateCol = createArrivalDateCol(sortHandler);
        Column<ScheduleProxy, String> tariffDateCol = createTariffDateCol(sortHandler);
        Column<ScheduleProxy, String> transportCompanyCol = createTransportCompanyCol(sortHandler);
        Column<ScheduleProxy, String> сurrencyCol = createCurrencyCol(sortHandler);
        Column<ScheduleProxy, String> scheduleDetailsBtnCol = createDetailsBtnCol();
        dataGrid.addColumn(idCol, "ID");
        dataGrid.addColumn(originCol, "Origin");
        dataGrid.addColumn(destinationCol, "Destination");
        dataGrid.addColumn(primeCostCol, "PrimeCost");
        dataGrid.addColumn(sellingRateCol, "SellingRate");
        dataGrid.addColumn(departureDateCol, "DepartureDate");
        dataGrid.addColumn(arrivalDateCol, "ArrivalDate");
        dataGrid.addColumn(tariffDateCol, "TariffDate");
        dataGrid.addColumn(transportCompanyCol, "TransportCompany");
        dataGrid.addColumn(сurrencyCol, "Currency");
        dataGrid.addColumn(scheduleDetailsBtnCol, "Details");
    }

    private Column<ScheduleProxy, String> createOriginCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {

            @Override
            public String getValue(ScheduleProxy object) {
                return object.getOrigin().getAddress();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return o1.getOrigin().getAddress().compareToIgnoreCase(o2.getOrigin().getAddress());
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createDestinationCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return object.getDestination().getAddress();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return o1.getDestination().getAddress().compareToIgnoreCase(o2.getDestination().getAddress());
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createPrimeCostCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getPrimeCost());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return String.valueOf(o1.getPrimeCost()).compareToIgnoreCase(String.valueOf(o2.getPrimeCost()));
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createSellingRateCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getSellingRate());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return String.valueOf(o1.getSellingRate()).compareToIgnoreCase(String.valueOf(o2.getSellingRate()));
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createDepartureDateCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getDepartureDate());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return String.valueOf(o1.getDepartureDate()).compareToIgnoreCase(String.valueOf(o2.getDepartureDate()));
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createArrivalDateCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getArrivalDate());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return String.valueOf(o1.getArrivalDate()).compareToIgnoreCase(String.valueOf(o2.getArrivalDate()));
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createTariffDateCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getTariffDate());
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return String.valueOf(o1.getTariffDate()).compareToIgnoreCase(String.valueOf(o2.getTariffDate()));
            }
        });
        return col;
    }

    private Column<ScheduleProxy, String> createTransportCompanyCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return object.getTransportCompany().getTitle();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return o1.getTransportCompany().getTitle().compareToIgnoreCase(o2.getTransportCompany().getTitle());
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createCurrencyCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new EditTextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return object.getCurrency().getTitle();
            }
        };
        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                return o1.getCurrency().getTitle().compareToIgnoreCase(o2.getCurrency().getTitle());
            }
        });
        return col;
    }
    private Column<ScheduleProxy, String> createIdCol(ColumnSortEvent.ListHandler<ScheduleProxy> sortHandler) {
        Column<ScheduleProxy, String> col = new Column<ScheduleProxy, String>(new TextCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return String.valueOf(object.getId());
            }
        };

        col.setSortable(true);
        sortHandler.setComparator(col, new Comparator<ScheduleProxy>() {
            @Override
            public int compare(ScheduleProxy o1, ScheduleProxy o2) {
                if (o1.getId() == o2.getId()) {
                    return 0;
                } else {
                    return o1.getId() > o2.getId() ? 1 : -1;
                }
            }
        });
        return col;
    }

    private Column<ScheduleProxy, String> createDetailsBtnCol() {
        Column<ScheduleProxy, String> detailsBtnCol = new Column<ScheduleProxy, String>(new ButtonCell()) {
            @Override
            public String getValue(ScheduleProxy object) {
                return "->";
            }
        };
        detailsBtnCol.setFieldUpdater(new FieldUpdater<ScheduleProxy, String>() {
            @Override
            public void update(int index, ScheduleProxy object, String value) {
                presenter.goTo(new ScheduleDetailsPlace(String.valueOf(dataProvider.getList().get(index).getId())));
            }
        });
        return detailsBtnCol;
    }

    public void setDataList(List<ScheduleProxy> dataList) {
        this.dataList = dataList;
        bindData(dataList);
    }

    public void bindData(List<ScheduleProxy> schedules) {
        dataProvider.setList(schedules);
        sortHandler.setList(dataProvider.getList());
        dataProvider.refresh();
    }
}
